
import { PAGECODE, AREA, UISTATUS, BUTTONID, FIELDS, CARDTABLEAREAIDS } from '../../constance';
import pageInfoClick from '../btnClicks/pageInfoClick';
import addBtnClick from '../btnClicks/addBtnClick';
import copyBtnClick from '../btnClicks/copyBtnClick';
import { getAreaAndBtnId } from '../utils/cardPageUtil';
import { lineBtnClick } from '../btnClicks';
import { getLangByResId } from '../../tool/multiLangUtil';
import { fiterAppPermissionOrg, fiterLrzxOrg } from '../../../../common/util/fiterUtils';

export default function(props) {
        props.createUIDom(
                {
                        pagecode: PAGECODE.cardPagecode
                },
                (data) => {
                        if (data) {
                                if (data.context) {
                                        this.contexts = data.context;
                                }
                                if (data.button) {
                                        let button = data.button;
                                        props.button.hideButtonsByAreas([ AREA.cardHead ]); // 隐藏所有按钮
                                        props.button.setOprationBtnsRenderStatus([ AREA.card_body_inner ], false);
                                        // props.cardTable.selectAllRows(AREA.cardTableId, false); // 取消浏览态表体勾选
                                        props.button.setButtons(button);
                                }
                                if (data.template) {
                                        let meta = data.template;
                                        modifierMeta.call(this, props, meta);
                                        props.meta.setMeta(meta, () => {
                                                // 设置默认数据
                                                setDefaultData.call(this, props);
                                        });
                                }
                        }
                }
        );
}

function modifierMeta(props, meta) {
        props.meta.setMeta(meta);
        meta[AREA.cardFormId].items.map(item => {
                if (item.attrcode === 'pk_org') { //财务组织
                        fiterAppPermissionOrg(props, AREA.cardFormId, item.attrcode, false);
                }
                else if (item.attrcode === 'vdef5') {//利润中心（部门档案）过滤
                        fiterLrzxOrg(props, AREA.cardFormId, item.attrcode, true, true);
                   }
        });

        let status = props.getUrlParam('status');
        referEvent.call(this, props, meta);
        meta[AREA.cardFormId].status = status;
        [
         AREA.cardBody_1,
         AREA.cardBody_2
    ].forEach((moduleId) => {
                meta[moduleId].status = status;
                // 操作列
                meta[moduleId].items.push({
                        label: getLangByResId(this, '4001PUBMESSAGE-0000006') /* 国际化处理： 操作*/,
                        itemtype: 'customer',
                        attrcode: 'opr',
                        width: '160px',
                        visible: true,
                        fixed: 'right',
                        render: (text, record, index) => {
                                if (props.getUrlParam('status') != UISTATUS.browse) {
                                        let { InnerAreaId, InnerBtnIds } = getAreaAndBtnId(
                                                moduleId,
                                                props.getUrlParam('status'),
                                                this.copyLineDatas[moduleId]
                                        );

                                        return props.button.createOprationButton(InnerBtnIds, {
                                                area: InnerAreaId,
                                                buttonLimit: 3,
                                                onButtonClick: (props, key) =>
                                                        lineBtnClick.call(this, moduleId, props, key, text, record, index)
                                        });
                                }
                        }
                });
        });

        return meta;
}
function referEvent(props, meta) {
        meta[AREA.cardFormId].items.map((item) => {
        if (item.attrcode == 'transtypepk') {
        // 交易类型
                item.queryCondition = () => {
                        let pk_org = (props.form.getFormItemsValue(AREA.cardFormId, 'pk_org') || {}).value;
                        return {
                                parentbilltype: 'TXDJ',
                                SCM_CONSIDERBUSITYPE: 'Y',
                                SCM_BUSIORG: pk_org,
                                GridRefActionExt: 'nccloud.web.scmpub.ref.TransTypeRefFilterUtils'
                                };
                        };
                }
        })
}

function setDefaultData(props) {
        let status = props.getUrlParam('status');
        let pk = props.getUrlParam('id');
        if (status == UISTATUS.add) {
                addBtnClick.call(this, props);
        } else if (status == UISTATUS.copy) {
                copyBtnClick.call(this, props, pk);
        } else {
                pageInfoClick.call(this, props, pk);
        }
        
        // 设置按钮可用性
        props.button.setButtonDisabled(
                [
                        BUTTONID.cardBody_1_DeleteLine,
                        BUTTONID.cardBody_2_DeleteLine,
                        BUTTONID.cardBody_1_CopyLine,
                        BUTTONID.cardBody_2_CopyLine
                ],
                true
        );
}
